package com.xuhai.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuhai.blog.admin.entity.ArticleCategoryEntity;
import com.xuhai.blog.admin.entity.ArticleEntity;
import com.xuhai.blog.admin.mapper.ArticleMapper;
import com.xuhai.blog.admin.service.ArticleCategoryService;
import com.xuhai.blog.admin.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhai.blog.admin.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2022-02-08
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleEntity> implements ArticleService {

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public IPage<ArticleEntity> getPage(int page, int limit, ArticleEntity article) {
        String startTime=null;
        String endTime=null;
        if(StringUtils.isNotBlank(article.getApplyTimeRange())){
            startTime=article.getApplyTimeRange().split(",")[0];
            endTime=article.getApplyTimeRange().split(",")[1];
        }
        QueryWrapper<ArticleEntity> qw = new QueryWrapper<ArticleEntity>()
                .like(StringUtils.isNotBlank(article.getTitle()),"title",article.getTitle())
                .eq(StringUtils.isNotBlank(article.getAuthor()),"author",article.getAuthor())
                .eq(article.getArticleStatus()!=null,"article_status",article.getArticleStatus())
                .eq(article.getStatus()!=null,"status",article.getStatus())
                .between(StringUtils.isNotBlank(article.getApplyTimeRange()),"apply_time",startTime,endTime);
        if(article.getReadCount()!=null){
            qw.orderByDesc("read_count","article_id");
        }else{
            qw.orderByDesc("article_id");
        }
        return this.page(new Page<>(page,limit),qw);
    }

    @Override
    public void update(ArticleEntity article) {
        /*添加操作则需要封装发布时间*/
        if(article.getArticleId()==null){
            article.setApplyTime(new Date());
        }
        /*添加或修改文章*/
        this.saveOrUpdate(article);
        /*删除该文章的关联信息*/
        articleCategoryService.deleteByArticleId(article.getArticleId());
        /*判断是否需要添加分类*/
        if(article.getCategoryIds()!=null&&article.getCategoryIds().length>0){
            /*初始化关联表集合*/
            List<ArticleCategoryEntity> acList=new ArrayList<>();
            /*数组转list*/
            for(Integer categoryId:article.getCategoryIds()){
                ArticleCategoryEntity ac=new ArticleCategoryEntity();
                ac.setArticleId(article.getArticleId());
                ac.setCategoryId(categoryId);
                acList.add(ac);
            }
            /*执行批量添加*/
            articleCategoryService.saveBatch(acList);
        }
    }

    @Override
    public void delete(Integer articleId) {
        this.removeById(articleId);
        articleCategoryService.deleteByArticleId(articleId);
        commentService.deleteByArticleId(articleId);
    }

    @Override
    public int getCount(ArticleEntity article) {
        return this.count(new QueryWrapper<ArticleEntity>().eq(article.getStatus()!=null,"status",article.getStatus()).eq(article.getArticleStatus()!=null,"article_status",article.getArticleStatus()));
    }

    @Override
    public IPage<ArticleEntity> getPageByCategoryId(int page, int limit,Integer categoryId, ArticleEntity article) {
        return articleMapper.getArticlePageByCategoryId(new Page<>(page,limit),article,categoryId);
    }

    @Override
    public void updateRead(Integer articleId) {
        articleMapper.updateRead(articleId);
    }
}
